import roleSerivce from "@/service/role.serivce";
import authMiddleware from "@/middleware/auth.middleware";
import { error } from "console";
import { Next } from "koa";
import { body, middlewares, routeConfig, z } from "koa-swagger-decorator";
import { Context } from "vm";
import { connect } from "http2";

const addSchema = z.object({
  role_name: z.string(),
  role_code: z.string(),
  description: z.string(),
  menuIds: z.array(z.string()).optional(),
  status: z.coerce.number().nullable().optional(),
});

const updateSchema = z.object({
  id: z.coerce.number(),
  role_name: z.string().nullable().optional(),
  role_code: z.string().nullable().optional(),
  description: z.string().nullable().optional(),
  menuIds: z.array(z.string()).optional(),
  status: z.coerce.number().nullable().optional(),
});
const geListSchema = z.object({
  current: z.coerce.number().min(1).default(1),
  pageSize: z.coerce.number().min(1).default(10),
  role_name: z.string().nullable().optional(), // 可选字符串
  status: z.coerce.number().nullable().optional(), // 可选数字
});

export class RoleController {
  @routeConfig({
    method: "post",
    path: "/role/add",
    summary: "增加角色",
    tags: ["角色"],
  })
  @body(addSchema)
  @middlewares([authMiddleware.verifyAuth])
  async add(ctx: Context, next: Next) {
    try {
      const res = await roleSerivce.add(ctx.request.body as API.roleItem);
      ctx.status = 200;
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "创建失败",
      };
      ctx.status = error.status || 400;
    }
  }
  @routeConfig({
    method: "delete",
    path: "/role/delete",
    summary: "批量删除",
    tags: ["角色"],
  })
  @body(
    z.object({
      ids: z.array(z.number()),
    })
  )
  @middlewares([authMiddleware.verifyAuth])
  async delete(ctx: Context, next: Next) {
    try {
      const res = await roleSerivce.delete(ctx.request.body.ids);
      // 返回通用响应格式
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 201;
    } catch (error: any) {
      // 错误响应格式
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "删除失败",
      };
      ctx.status = error.status || 400;
    }
  }
  @routeConfig({
    method: "put",
    path: "/role/update",
    summary: "更新角色",
    tags: ["角色"],
  })
  @body(updateSchema)
  @middlewares([authMiddleware.verifyAuth])
  async update(ctx: Context, next: Next) {
    try {
      console.log("ok", ctx.request.body);
      const res = await roleSerivce.update(ctx.request.body as API.roleItem);
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 201;
    } catch (error: any) {
      // 错误响应格式
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "更新失败",
      };
      ctx.status = error.status || 400;
    }
  }
  @routeConfig({
    method: "get",
    path: "/role/list",
    summary: "获取角色列表",
    tags: ["角色"],
    request: {
      query: geListSchema,
    },
  })
  @middlewares([authMiddleware.verifyAuth])
  async getList(ctx: Context, next: Next) {
    try {
      const res = await roleSerivce.getList(ctx.request.query as API.roleList);
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 200;
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error || "获取列表失败",
      };
      ctx.status = error.status || 400;
    }
  }
}
